package th.kmutt.se.is;

import java.text.DecimalFormat;

import th.kmutt.se.is.model.Room;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RoomDetail extends FragmentActivity implements OnClickListener{
	
	Room room;
	
	Bundle bundle;
	
	ViewPager detailPager;
	Button navBtn1, navBtn2;
	TextView pageTitle, roomFloor, roomPrice, roomDeposit, roomWaterMeter, roomElectMeter;
	ImageButton btnMenu1;
	RelativeLayout actionBar;
	

	final int roomStatusText[] = {0, R.string.empty, R.string.normal, R.string.close};
	final int pageNav[] = {R.drawable.nav_def, R.drawable.nav_act_empty, R.drawable.nav_act_normal, R.drawable.nav_act_close};
	final int actionBarBg[] = {R.drawable.actionbar_bg, R.drawable.actionbar_bg_empty, R.drawable.actionbar_bg_normal, R.drawable.actionbar_bg_close};
	final int btnMenuBg[] = {R.drawable.actionbar_btn, R.drawable.actionbar_btn_empty, R.drawable.actionbar_btn_normal, R.drawable.actionbar_btn_close}; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_room_detail);
		init();
	}

	private void init() {
		room = new Room(this);
		bundle = getIntent().getExtras();
		
		actionBar = (RelativeLayout) findViewById(R.id.actionBar);
		pageTitle = (TextView) findViewById(R.id.pageTitle);
		btnMenu1 = (ImageButton) findViewById(R.id.btnMenu2);
		navBtn1 = (Button) findViewById(R.id.navBtn1);
		navBtn2 = (Button) findViewById(R.id.navBtn2);

		roomFloor = (TextView) findViewById(R.id.roomFloor);
		roomPrice = (TextView) findViewById(R.id.roomPrice);
		roomDeposit = (TextView) findViewById(R.id.roomDeposit);
		roomWaterMeter = (TextView) findViewById(R.id.roomWaterMeter);
		roomElectMeter = (TextView) findViewById(R.id.roomElectMeter);
		
		detailPager = (ViewPager) findViewById(R.id.detailPager);
		RoomPagerAdapter roomPagerAdapter = new RoomPagerAdapter(getSupportFragmentManager());
		detailPager.setAdapter(roomPagerAdapter);
		
		process();
	}
	
	private void process(){
		room.getRoomById(bundle.getInt("roomID"));
		DecimalFormat moneyFormat = new DecimalFormat("#,##0.00");
		DecimalFormat meterFormat = new DecimalFormat("000000");
		if(room.getId()>0){
			
			pageTitle.setText(room.getName() + " " + (room.getStatus()>0?"(" + getString(roomStatusText[room.getStatus()]) + ")" : ""));
			actionBar.setBackgroundResource(actionBarBg[room.getStatus()]);
			btnMenu1.setBackgroundResource(btnMenuBg[room.getStatus()]);
			
			roomFloor.setText(room.getFloor()+"");
			roomPrice.setText(moneyFormat.format(room.getPrice()));
			roomDeposit.setText(moneyFormat.format(room.getDepositPrice()));
			roomWaterMeter.setText(meterFormat.format(room.getWaterMeter()));
			roomElectMeter.setText(meterFormat.format(room.getElectMeter()));

			navBtn1.setBackgroundResource(pageNav[room.getStatus()]);
			navBtn2.setBackgroundResource(pageNav[0]);
		}
	}

	@Override
	public void onClick(View v) {
		
	}
	
	private class RoomPagerAdapter extends FragmentStatePagerAdapter {
        public RoomPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
        	Fragment fragment = null;
        	switch(position){
	    		case 0:{fragment = new RoomChargesList();}break;
	    		case 1:{fragment = new RoomList();}break;
        	}
            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }
    }
	
}
